package esi.t2no1.web.contract;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import esi.t2no1.dao.contract.ContractDAO;
import esi.t2no1.dao.contract.ContractDTO;

/**
 * Controller that handles contract related views
 * 
 * @author Taavi Pauskar
 *
 */
@Controller
@RequestMapping(value = "/contract")
public class ContractController {
	private ContractDAO contractDAO;

	@RequestMapping(method = RequestMethod.GET)
	public String getList(Model model) {
		model.addAttribute("contracts", contractDAO.getContracts());
		return "contract/list";
	}

	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public String getView(@PathVariable Long id, Model model) {
		ContractDTO contract = contractDAO.getContract(id);
		if (contract == null) {
			model.addAttribute("error", "contract.notFound");
			return "contract/view";
		}
		model.addAttribute(contract);
		return "contract/view";
	}

	@Autowired
	public void setContractDAO(ContractDAO contractDAO) {
		this.contractDAO = contractDAO;
	}
}
